<?php
/**
 * Clase Base FN para BaseMrpOrdenproduccionDetFN
 *
 * @package    
 * @subpackage BaseMrpOrdenproduccionDetFN
 * @author     hans
 * @version    SVN:: BaseMrpOrdenproduccionDetFN.class.php,v 1.1 2012-09-18 22:59:53 Hans.parra Exp $
 */
class BaseMrpOrdenproduccionDetFN extends MrpOrdenproduccionDetBN
{
  private static $fieldNames = array (
    'OrdenId'=>'pdo_ordenId', 'ArticuloId'=>'itm_articuloId', 'Estado'=>'mrpopd_status', 'Cantidad'=>'mrpopd_Qty', 'Referencia'=>'mrpopd_Ref', 'CreaUsr'=>'mrpopd_creaUsr', 'CreaFecha'=>'mrpopd_creaFecha', 'ModifUsr'=>'mrpopd_modifUsr', 'ModifFecha'=>'mrpopd_modifFecha'  
  );

  public function select()
  {
    try
    {
      /*$tamPag = !property_exists($this, 'tamPag')?9999:$this->tamPag;
      $pagina = !property_exists($this, 'page')||$this->page==""?1:$this->page;
      $numeroRegistros = $this->selectNumRows();
      $limitInf = ($pagina-1)*$tamPag;*/

      $where = $this->buildConsult();
      //$orden = $this->buildOrderByField();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      //$sql = "SELECT pdo_ordenId, itm_articuloId, mrpopd_status, mrpopd_Qty, mrpopd_Ref, mrpopd_creaUsr, mrpopd_creaFecha, mrpopd_modifUsr, mrpopd_modifFecha FROM mrp_ordenproduccionDet WHERE 1 ".$where." ".$orden."  LIMIT ".$limitInf.",".$tamPag;
      $sql = "SELECT pdo_ordenId, itm_articuloId, mrpopd_status, mrpopd_Qty, mrpopd_Ref, mrpopd_creaUsr, mrpopd_creaFecha, mrpopd_modifUsr, mrpopd_modifFecha FROM mrp_ordenproduccionDet WHERE 1 ".$where;
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      $a_tmp = array();
      if($num_rows>0){
        while ($o_valor = mysql_fetch_object($result)) {
          $a_tmp[] = $this->insertObjectBN($o_valor);
        }
      }
      
      return count($a_tmp)>0?$a_tmp:null;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }


  public function selectExtend()
  {
    try
    {
      $where = $this->buildConsult();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $where = str_replace("pdo_ordenid","D.pdo_ordenid",$where);
      $sql = "
          SELECT 
            D.pdo_ordenId, 
            D.itm_articuloId, 
            mrpopd_status, 
            mrpopd_Qty, 
            mrpopd_Ref, 
            mrpopd_creaUsr, 
            mrpopd_creaFecha, 
            mrpopd_modifUsr, 
            mrpopd_modifFecha ,
            itm_descripcion,
            stk_cantFisico,
            stk_cantDispon
          FROM mrp_ordenproduccionDet D
            LEFT JOIN im_articulo A ON D.itm_articuloId = A.itm_articuloId
            LEFT JOIN mrp_ordenproduccion H ON D.pdo_ordenId = H.pdo_ordenId
            LEFT JOIN im_stock S ON 
                H.pdo_almacenId = stk_almId 
                AND D.itm_articuloId = stk_artId                
          WHERE 1 ".$where;
    //  exit($sql);
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      $a_tmp = array();
      if($num_rows>0){
        while ($o_valor = mysql_fetch_object($result)) {
          $a_tmp[] = $this->insertObjectBN($o_valor);
        }
      }
      
      return count($a_tmp)>0?$a_tmp:null;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }  
  
  
  
  public function selectNumRows()
  {
    try
    {
      $where = $this->buildConsult();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT pdo_ordenId, itm_articuloId, mrpopd_status, mrpopd_Qty, mrpopd_Ref, mrpopd_creaUsr, mrpopd_creaFecha, mrpopd_modifUsr, mrpopd_modifFecha FROM mrp_ordenproduccionDet WHERE 1 ".$where;
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      
      return $num_rows;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildConsult()
  {
    try
    {
      $where = "";

      if($this->getOrdenId()){ $where .= " AND pdo_ordenid = '".$this->getOrdenId()."'"; }
      if($this->getArticuloId()){ $where .= " AND itm_articuloid = '".$this->getArticuloId()."'"; }
      if($this->getEstado()){ $where .= " AND mrpopd_status = '".$this->getEstado()."'"; }
      if($this->getCantidad()){ $where .= " AND mrpopd_qty = '".$this->getCantidad()."'"; }
      if($this->getReferencia()){ $where .= " AND mrpopd_ref = '".$this->getReferencia()."'"; }
      if($this->getCreaUsr()){ $where .= " AND mrpopd_creausr = '".$this->getCreaUsr()."'"; }
      if($this->getCreaFecha()){ $where .= " AND mrpopd_creafecha = '".$this->getCreaFecha()."'"; }
      if($this->getModifUsr()){ $where .= " AND mrpopd_modifusr = '".$this->getModifUsr()."'"; }
      if($this->getModifFecha()){ $where .= " AND mrpopd_modiffecha = '".$this->getModifFecha()."'"; }
      return $where;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildOrderByField()
  {
    try
    {
      $order_by = "";

      if(property_exists($this,'sortCampos')){
      if($this->sortCampos != ""){
        $a_valCampos = explode(";", $this->sortCampos);
        $a_valtype   = explode(";", $this->sortType);
        $order_by = " ORDER BY ";
        $a_order = array();
        foreach ($a_valCampos as $key => $val){
          $sTmpOrder = "";
          if(array_key_exists($val, self::$fieldNames)){
            $sTmpOrder .= self::$fieldNames[$val];
            switch ($a_valtype[$key]){
              case 1: $sTmpOrder .= " ASC"; break;
              case 2: $sTmpOrder .= " DESC"; break;
            }
            $a_order[] = $sTmpOrder;
          }
        }
        $order_by .= implode(", ", $a_order);
      }
      }
      return $order_by;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Búsqueda del registro por la llave primaria */
  public function selectByPK()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT pdo_ordenId, itm_articuloId, mrpopd_status, mrpopd_Qty, mrpopd_Ref, mrpopd_creaUsr, mrpopd_creaFecha, mrpopd_modifUsr, mrpopd_modifFecha FROM mrp_ordenproduccionDet WHERE 1 AND pdo_ordenId='".$this->getOrdenId()."' AND itm_articuloId='".$this->getArticuloId()."'; ";
        $result = mysql_query($sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      

      return $o_Propel?$this->insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con métodos de acceso */
  private function insertObjectBN($o_propel){
    $o_BaseMrpOrdenproduccionDetFN = new MrpOrdenproduccionDetBN();
    $o_BaseMrpOrdenproduccionDetFN->setOrdenId($o_propel->pdo_ordenId);
    $o_BaseMrpOrdenproduccionDetFN->setArticuloId($o_propel->itm_articuloId);
    $o_BaseMrpOrdenproduccionDetFN->setEstado($o_propel->mrpopd_status);
    $o_BaseMrpOrdenproduccionDetFN->setCantidad($o_propel->mrpopd_Qty);
    $o_BaseMrpOrdenproduccionDetFN->setReferencia($o_propel->mrpopd_Ref);
    $o_BaseMrpOrdenproduccionDetFN->setCreaUsr($o_propel->mrpopd_creaUsr);
    $o_BaseMrpOrdenproduccionDetFN->setCreaFecha($o_propel->mrpopd_creaFecha);
    $o_BaseMrpOrdenproduccionDetFN->setModifUsr($o_propel->mrpopd_modifUsr);
    $o_BaseMrpOrdenproduccionDetFN->setModifFecha($o_propel->mrpopd_modifFecha);
    
    $o_BaseMrpOrdenproduccionDetFN->setArticuloNombre($o_propel->itm_descripcion);
    $o_BaseMrpOrdenproduccionDetFN->setStockFisico($o_propel->stk_cantFisico);
    $o_BaseMrpOrdenproduccionDetFN->setStockDisponible($o_propel->stk_cantDisponible);
    return $o_BaseMrpOrdenproduccionDetFN;
  }
}